package com.zkingsoft.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.bean.BizIncomeLog;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.dao.BizIncomeLogDao;
import com.zkingsoft.service.BizIncomeLogService;

/**
 * @description service接口实现类(收入流水表)
 * @author 何明
 * @date 2018-07-17 21:18
 */
@Service
public class  BizIncomeLogServiceImpl implements  BizIncomeLogService{

	
	@Autowired
	private BizIncomeLogDao bizIncomeLogDao;
	
	
	@Override
	public int add(BizIncomeLog bizIncomeLog){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizIncomeLog.setCreateBy(loginUser.getSuName());
		bizIncomeLog.setUpdateBy(loginUser.getSuName());
		bizIncomeLog.setIncId(UUIDUtil.getRandomID());
		return bizIncomeLogDao.insert(bizIncomeLog);
		
	}
	
	@Override
	public int batchAdd(List<BizIncomeLog>  bizIncomeLogList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizIncomeLogList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizIncomeLogList.size() ? bizIncomeLogList.size() : end;
			List<BizIncomeLog> insertList = bizIncomeLogList.subList(begin, end);
			num += bizIncomeLogDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizIncomeLog oldBizIncomeLog
	,BizIncomeLog newBizIncomeLog){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizIncomeLog, newBizIncomeLog)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizIncomeLog, newBizIncomeLog);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizIncomeLog);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("incId", oldBizIncomeLog.getIncId());
			bizIncomeLogDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizIncomeLog bizIncomeLog){
	
		return bizIncomeLogDao.updateByModel(bizIncomeLog);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizIncomeLogDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String incId){
	
		return bizIncomeLogDao.deleteById(incId);
	
	}
	
	@Override
	public int removeByModel(BizIncomeLog bizIncomeLog){
	
		return bizIncomeLogDao.deleteByModel(bizIncomeLog);
	
	}
	
	
	@Override
	public List<BizIncomeLog> findInPage(BizIncomeLog bizIncomeLog,  PaginationVO pageVo){
	
		return bizIncomeLogDao.selectInPage(bizIncomeLog , pageVo);
	
	}
	
	@Override
	public List<BizIncomeLog> findByModel(BizIncomeLog bizIncomeLog){
	
		return bizIncomeLogDao.selectByModel(bizIncomeLog);
	
	}
	
	@Override
	public int  findTotal(BizIncomeLog bizIncomeLog){
	
		return bizIncomeLogDao.selectTotalRecord(bizIncomeLog);
	
	}
	
	@Override
	public BizIncomeLog  findById(String incId){
	
		return bizIncomeLogDao.selectById(incId);
	
	}

   	
	
	
}